package com.ml.mall.service.payinfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.ipay.Ipay88CallBackDto;
import com.ml.mall.bean.req.order.OrderPaymentDataQueryDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.payinfo.SysPayinfo;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.service.payinfo.SysPayinfoService;
import com.ml.mall.mapper.SysPayinfoMapper;
import com.ml.mall.utils.SdkIpay88Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 *
 */
@Service
public class SysPayinfoServiceImpl extends ServiceImpl<SysPayinfoMapper, SysPayinfo>
    implements SysPayinfoService{

    @Override
    public Map<String, String> createOrderPay(Long oid, BigDecimal orderAllFee, SysUser user) throws NoSuchAlgorithmException {
        SysPayinfo sysPayinfo = new SysPayinfo();
        sysPayinfo.setMoney(orderAllFee);
        sysPayinfo.setPayId(oid);
        sysPayinfo.setPayMethod("OrderPay");
        sysPayinfo.setState(CommonConsts.ORDER_STATE_PENDING);
        sysPayinfo.setType(CommonConsts.PAY_ORDER_TYPE_ORDER);

        save(sysPayinfo);

        return SdkIpay88Utils.doRequestToIpay88(sysPayinfo, user);
    }

    @Override
    public SysPayinfo queryByPayId(Long payId) {
        LambdaQueryWrapper<SysPayinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPayinfo::getPayId, payId);
        return getOne(queryWrapper);
    }

    @Override
    public boolean updateByCallBackDto(Ipay88CallBackDto dto) {
        SysPayinfo sysPayinfo = new SysPayinfo();
        LambdaUpdateWrapper<SysPayinfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysPayinfo::getPayId,dto.getRefNo());

        sysPayinfo.setState(Integer.valueOf(dto.getStatus()));
        sysPayinfo.setTransId(dto.getTransId());
        sysPayinfo.setPaymentId(dto.getPaymentId());
        sysPayinfo.setCcName(dto.getCCName());
        sysPayinfo.setCcNo(dto.getCCNo());
        sysPayinfo.setSBankname(dto.getS_bankname());
        sysPayinfo.setSCountry(dto.getS_country());

        return update(sysPayinfo, updateWrapper);
    }
}




